package com.cloud.cmt.usermanage.controller;

import com.angela.cloud.common.base.ResultMessage;
import com.angela.cloud.common.constant.ResultCodeConstant;
import com.angela.cloud.common.exception.BusinessException;
import com.cloud.cmt.usermanage.base.BaseVaule;
import com.cloud.cmt.usermanage.base.PagePo;
import com.cloud.cmt.usermanage.base.PageRequestFactory;
import com.cloud.cmt.usermanage.entity.Employee;
import com.cloud.cmt.usermanage.service.EmployeeRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.postgresql.util.PSQLException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: wcy
 * Date: 2017/10/17
 * Time: 16:14
 */
@RestController
@RequestMapping("/employee")
@Api(value = "/员工管理", description = "员工管理-员工增删查改等-by wcy")
public class EmployeeCtl {

    @Resource
    EmployeeRepository employeeRepository;

    @ApiOperation(value = "通过id获取信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResultMessage<Employee> getUser(@PathVariable long id) {
        return ResultMessage.success(employeeRepository.findOne(id));
    }

    @ApiOperation(value = "保存信息")
    @PostMapping(value = "")
    @ResponseBody
    public ResultMessage<Employee> save(@RequestBody @Validated({Employee.SAVE.class}) Employee employee) throws BusinessException {
        ResultMessage resultMessage = new ResultMessage();
        List<Employee> employees = employeeRepository.findByEmployeeNum(employee.getEmployeeNum());
        if (employees.size() > 0) {
            throw new BusinessException(8500 + "", "工号:" + employee.getEmployeeNum() + "已经存在，请使用其他工号", 1);
        }
        employee = employeeRepository.save(employee);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setId(employee.getId() + "");

        return resultMessage;
    }

    @ApiOperation(value = "更新信息")
    @PutMapping(value = "")
    @ResponseBody
    public ResultMessage<Employee> update(@RequestBody Employee Employee) throws BusinessException {
        ResultMessage resultMessage = new ResultMessage();
        try{
            employeeRepository.saveAndFlush(Employee);
            resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        }catch (Exception e){
          throw new BusinessException(8500,"更新报错，员工工号不能重复！！");
        }

        return resultMessage;
    }

    @ApiOperation(value = "通过id删除")
    @DeleteMapping(value = "/{id}")
    public ResultMessage remove(@PathVariable("id") Long id) {
        ResultMessage resultMessage = new ResultMessage();
        employeeRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        return resultMessage;
    }

    @ApiOperation(value = "通过条件查询员工信息")
    @PostMapping(value = "/findPage")
    public ResultMessage<List<Employee>> find(@RequestBody PagePo<BaseVaule> pagePo) {
        String value = pagePo.getFields().getValue();
        return ResultMessage.success(employeeRepository.findByEmployee("%" + value + "%", PageRequestFactory.getPageInfo(pagePo)));
    }
}
